# 构建阶段 - 使用标准 JDK 镜像
FROM maven:3.6.1-jdk-8-alpine AS build

WORKDIR /app
ARG PROJECT_NAME=app-api

COPY . .
# 创建模块目录并复制模块 pom.xml
# RUN mkdir -p ${PROJECT_NAME}
# COPY ./${PROJECT_NAME}/pom.xml ./${PROJECT_NAME}/
COPY .mvn/ .mvn/
# 下载依赖
RUN --mount=type=cache,target=/root/.m2/repository \
mvn -B -pl ${PROJECT_NAME} dependency:resolve-plugins dependency:go-offline

# COPY . .
# 构建项目
RUN --mount=type=cache,target=/root/.m2/repository \
mvn -B -pl ${PROJECT_NAME} package -DskipTests

# RUN apk add --no-cache wget
# # 下载 OpenTelemetry Java Agent
# RUN wget -O opentelemetry-javaagent.jar \
#     https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v1.28.0/opentelemetry-javaagent.jar
# COPY opentelemetry-javaagent.jar opentelemetry-javaagent.jar
# 运行时阶段 - 使用 Alpine JDK
FROM azul/zulu-openjdk-alpine:8u452-jdk

WORKDIR /app
ENV PROJECT_NAME=app-api

COPY opentelemetry-javaagent.jar .
COPY --from=build /app/${PROJECT_NAME}/target/${PROJECT_NAME}.jar app.jar
# 设置环境变量（可选）
ENV JAVA_TOOL_OPTIONS="-javaagent:opentelemetry-javaagent.jar"
# ENV OTEL_JAVAAGENT_JAR=opentelemetry-javaagent.jar
ENV OTEL_EXPORTER_OTLP_PROTOCOL=grpc
ENV OTEL_EXPORTER_OTLP_ENDPOINT=http://alloy:4319
# traces
ENV OTEL_TRACES_EXPORTER=otlp
# metrics
ENV OTEL_METRICS_EXPORTER=none
# ENV OTEL_METRIC_EXPORT_INTERVAL=15000
# logs
ENV OTEL_LOGS_EXPORTER=otlp

# ENV OTEL_JAVAAGENT_LOGGING_LEVEL=DEBUG
# ENV OTEL_INSTRUMENTATION_COMMON_EXPERIMENTAL_CONTROLLER_TELEMETRY_ENABLED=true
ENV OTEL_LOG_LEVEL=INFO
ENV OTEL_JAVAAGENT_LOGGING=application
# ENV OTEL_SDK_DISABLED=true

CMD java $JAVA_OPTS \
-Dspring.profiles.active=$profile \
# -Dotel.instrumentation.micrometer.enabled=true \
-Dotel.resource.attributes=deployment.env=$profile \
-jar app.jar
